package com.ljd.controller;


import com.ljd.entity.EduCourse;
import com.ljd.entity.page.PageResponse;
import com.ljd.entity.vo.CourseInfoVo;
import com.ljd.entity.vo.CoursePublishVo;
import com.ljd.service.EduCourseService;
import com.ljd.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ljd
 * @since 2021-12-10
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;
    @ApiOperation(value = "新增课程")
    @PostMapping("saveCourse")
    public R saveCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        if(!StringUtils.isEmpty(id)) {
            return R.ok().data("courseId",id);
        }else {
            return R.error().message("添加课程失败");
        }
    }
    @ApiOperation(value = "修改课程")
    @PostMapping("updateCourse")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }
    @ApiOperation(value = "课程列表")
    @GetMapping("getCourse/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    @ApiOperation(value = "课程确认信息")
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(courseId);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }
    @ApiOperation(value = "发布课程")
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        eduCourseService.publishCourse(courseId);
        return R.ok();
    }
    @ApiOperation(value = "课程列表")
    @GetMapping("getCourseList")
    public R getCourseList(){
        List<EduCourse> courses = eduCourseService.list(null);
        return R.ok().data("courses", courses);
    }
    @ApiOperation(value = "课程列表")
    @GetMapping("getCoursesByCondition/{current}/{pageSize}")
    public R getCoursesByCondition(@PathVariable Integer current,@PathVariable Integer pageSize,
                                   @RequestParam(value = "title", required = false) String title ,
                                   @RequestParam(value = "status",required = false) String status){
        PageResponse<EduCourse> eduCourses = eduCourseService.listByCondition(current,pageSize,title,status);
        return R.ok().data("pageCourses", eduCourses);
    }
    @ApiOperation(value = "删除课程")
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.deleteCourse(courseId);
        return R.ok();
    }
}

